package com.meiyou.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.meiyou.service.AliYunService;
import com.meiyou.service.ShareService;
import com.meiyou.service.UserService;
import com.meiyou.utils.Context;
import com.meiyou.utils.Msg;
import com.meiyou.utils.ShareCodeUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "阿里云接口调用控制器", tags = { "阿里云接口" })
public class ApiSendController {
    
	@Autowired
	AliYunService aliyun;
	@Autowired
	UserService userservice;
	@Autowired
	ShareService shareService;
	
	@RequestMapping(value="sendMsg",method=RequestMethod.POST)
	@ApiOperation(value = "发送短信验证码接口")
	public @ResponseBody Msg sendMsg(String phone,int msgtype,HttpServletRequest req) {
		if(phone==null) {
			System.out.println("空");
		}
		System.out.println(phone+","+msgtype);
		try {
			Msg msg = aliyun.sendMsg(phone, msgtype,req);
			return msg;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			Msg msg = Msg.fail();
			msg.setMsg("错误！短信类型应为数字");
			e.printStackTrace();
			return msg;
		}
	}
	
	
	@RequestMapping(value="getSession",method=RequestMethod.GET)
	public @ResponseBody String getSession(HttpServletRequest req,String phone) {
		if(req.getSession().getAttribute(phone)!=null) {
			return req.getSession().getAttribute(phone).toString();
		}
		return null;
	}
	
	
	
	@RequestMapping(value="updPwd",method=RequestMethod.GET)
	@ApiOperation(value = "短信验证忘记密码修改")
	public @ResponseBody Msg updPwd(HttpServletRequest req,String phone,String mycode,String newpwd) {
		Msg msg;
		if(req.getSession().getAttribute(phone)!=null) {
			String code = req.getSession().getAttribute(phone).toString();
			if(code.equals(mycode)) {
				return userservice.updPwdByPhone(newpwd, phone);
			}else {
				msg = Msg.fail();
				msg.setMsg("验证码错误");
				return msg;
			}		
		}else {
			msg = Msg.fail();
			msg.setMsg("未发送验证码");
			return msg;
		}
	}

	/**
	 * 验证码注册
	 * @param code
	 * @param phone
	 * @param req
	 * @return
	 */
	@RequestMapping(value="regist",method=RequestMethod.POST)
	@ApiOperation(value = "用户验证码注册")
	public @ResponseBody Msg regist(String code,String phone,String password,String sharecode,HttpServletRequest req) {
		System.out.println("注册请求。号码"+phone+"，验证码"+code);
	
		if(getSession(req,phone)==null) {
		     Msg msg = Msg.fail();
		     msg.setMsg("没有发送验证码");
		     return msg;
		}
		else if(req.getSession().getAttribute(phone).toString().equals(code)){	
			
			
			try {
				String uccount = ShareCodeUtil.getRamdomCount();
				Msg result = userservice.insUserRegist(phone,password,uccount);
				if(result.getCode()==100) {
					
					if(sharecode!=null) {
			    		shareService.selShareCode(sharecode).getCode();
			    	}
					req.getSession().setAttribute("user", uccount);
					Msg msg = Msg.success();
					return msg;	
				}
				else {
					return result;	
				}
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				Msg msg= Msg.fail();
				msg.setMsg("服务器异常");
				return msg;
			} 
			
		}
		else {
			Msg msg = Msg.fail();
			msg.setMsg("验证码错误");
			return msg;
		}		
	}
	
	
	
	/**
	 * 验证码登录
	 */
	 @RequestMapping(value="login",method=RequestMethod.POST)
	 @ApiOperation(value = "用户验证码登录")
	 public @ResponseBody Msg login(String code,String phone,HttpServletRequest req,double jindu,double weidu) {
		 System.out.println("用户登录:手机"+phone+"经度"+jindu);
		 if(getSession(req,phone)==null) {		
			     Msg msg = Msg.fail();
			     msg.setMsg("没有发送验证码");
			     return msg;
		 }else if(req.getSession().getAttribute(phone).toString().equals(code)) {
			 String name = userservice.selUserCount(phone,jindu,weidu);
			 if(name!=null) {
				 req.getSession().setAttribute("user",name);
				 System.out.println("user为"+req.getSession().getAttribute("user"));
				 Msg msg = Msg.success();
				 return msg;
			 }
			 else {
				 Msg msg = Msg.fail();
				 msg.setMsg("手机号未注册");
				 return msg;
			 }
		 }
		 Msg msg = Msg.fail();
		 return msg;	
	 }
}
